Skip to content

Health: FastAPI checks added where a router is included - #15

Merged
haydenschmitty merged 1 commit into
haydenschmitty:mainfrom
stevensiht:fix/fastapi-include-router-guards
Oct 30, 2026
Merged

haydenschmitty merged 1 commit into
haydenschmitty:mainfrom
stevensiht:fix/fastapi-include-router-guards

Conversation

@stevensiht

Copy link
Copy Markdown
Contributor

What was wrong

Many FastAPI apps add their sign-in check once, where each router is included, rather than on every endpoint:

app.include_router(admin.router, dependencies=[Depends(require_admin)])

Health didn't see this, so every endpoint of such a router was reported as "Anyone could change data through …". On a real FastAPI + React app (one repo, ~170 endpoints, every router guarded this way and held by a route-guard test) the map showed 3 high, 5 medium and 5 low false alarms. After this change it shows none, and its real "very large files" item stays.

The fix

  1. extract.mjs: Python include_router(...) now reads dependencies=[Depends(x), Security(y)] into the mount's mw. The arguments are read up to the call's own closing bracket, because Depends(...) has brackets of its own, and an include can span several lines. resolveRoutes already carries mount middleware down to each route (mountMw), and Health already honours it for Express's app.use('/admin', requireStaff, router).

  2. health.mjs: the mount check uses isGuardDep instead of isGuardName, so a database session added at the include (Depends(get_db_session)) still isn't a sign-in check. For Express middleware names this changes nothing in practice.

  3. routes.mjs: two cases where an include pointed at the wrong file, both found on the same app:

    • from app.routes import campaigns as campaign_routes then include_router(campaign_routes.router) matched no import, so the router lost its prefix as well as its checks;
    • from app.platform import pricing matched a platform router first, because the module ends in the same name.

    mountTarget now looks first for the import that binds the mounted name in the file, then falls back to the old matching.

Tests

A new sample app, tests/fixtures/fastapi-include-router, and a test in tests/health.test.mjs cover:

  • a guarded include;
  • a guarded include over several lines, beside a database session, with the router imported under another name and a prefix;
  • a package that shares a router's name, imported first;
  • an include whose only dependency is a database session, which must stay flagged.

Without the fix, the test fails (DELETE /items/{item_id} and GET /staff/report reported open, and the renamed router's prefix missing). With it, the test passes.

npm test: 69 of 70 pass. The one failure, ask.test.mjs "with only Codex installed…", fails the same way on main on macOS: the temp folder comes back as /private/var/… against /var/…. It's unrelated to this change. claude plugin validate . passes. A line is added under "Unreleased" in CHANGELOG.md.

🤖 Generated with Claude Code

A FastAPI app often adds its sign-in check once, where each router is
included:

    app.include_router(admin.router, dependencies=[Depends(require_admin)])

The Python reader kept only the include's prefix, so every endpoint of
such a router was reported as open to anyone. It now reads the include's
dependencies into the mount's middleware, which Health already honours
for Express (app.use('/admin', requireStaff, adminRouter)). The mount
check now uses isGuardDep, so a database session added there
(Depends(get_db_session)) still isn't a sign-in check.

Finding the router by the name main.py binds:
- `from app.routes import campaigns as campaign_routes` binds
  campaign_routes, which matched no import, so the router lost its
  prefix and its checks;
- `from app.platform import pricing` matched a `platform` router first,
  because its module ends in the same name.
The import that binds the mounted name in the file now wins, before the
old fallbacks.

A sample app under tests/fixtures/fastapi-include-router covers all
three: guarded includes (one over several lines beside a database
session), a renamed import, a package sharing a router's name, and an
include whose only dependency is a database session, which stays open.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@haydenschmitty

Copy link
Copy Markdown
Owner

Thanks @stevensiht, nicely done, and the test with a database session alone is exactly the right negative case. Merged and released in 0.3.1.

One edge case for later: a list nested inside dependencies=[…], like [Security(auth, scopes=["a"]), Depends(require_admin)], stops at the first ], so the second check is missed. This doesn't cover #7's three patterns, so that one stays open if you'd like to take it on. The macOS /private/var test failure you hit is a separate bug on our side; we'll track it on its own.

haydenschmitty added a commit that referenced this pull request Oct 30, 2026
The temp folder is /var/… but Node runs serve.mjs from /private/var/…. Found by @stevensiht in #15.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants